|
If you can't view the Datasheet, Please click here to try to view without PDF Reader . |
|
Datasheet File OCR Text: |
november 2012 doc id 17566 rev 3 1/13 AN3225 application note audio software codec for stm8l15xxx and stm8al31xx/stm8al3lxx introduction this application note describes a simple and easy-to-use solution to help design and develop products with an audio output, for example, toys, alarms, door bells, and white goods. audio data generally require the storage of a lot of memory. however, the memory space needed to preserve such data can be reduced by compressing the audio data via a specified compression method. processing these data is usually a complex task requiring powerful microcontrollers such as dsp or audi o chip. normally, 8-bit microcontrollers do not offer either a large memory or a high computing performance. the software solution offered in this application note reconstructs audio signals from compressed samples. a simple audio codec based on an adaptive differential pulse coded modulation (adpcm) algorithm is used and advantage is taken of the advanced stm8l/stm8al core with its 3-staged pipeline and 16 mips peak performance. the digital samples are converted to an analog signal by an on-chip, 12-bit, digital-to-analog converter (dac). this offers good overall audio quality, yet is a cost-effective solution for 8- bit microcontroller applications. the battery-powered application benefits from the ultra low-power consumption of stm8l/stm8al mi crocontrollers. the example firmware and application hardware design are provided with this application note to enable easy porting of the offered solution to the final application. ta bl e 1 summarizes the products covered by this application note. table 1. applicable products product family part numbers microcontrollers ? stm8l151x4, stm8l151x6, stm8l152x4, stm8l152x6 ? stm8l151x8, stm8l152x8, stm8l151r6, stm8l152r6, stm8l162r8, stm8l162m8 ? stm8al313x, stm8al314x, stm8al316x, stm8al3l4x, stm8al3l6x www.st.com
contents AN3225 2/13 doc id 17566 rev 3 contents 1 adpcm codec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.1 adaptive differential pulse coded modulation (adpcm) . . . . . . . . . . . . . . . 3 1.2 interactive multimedia association (ima) adpcm . . . . . . . . . . . . . . . . . . . 3 1.3 ima adpcm decompression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.4 ima adpcm compression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2 audio data encoding and storing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2.1 internal flash memory used for audio data st oring . . . . . . . . . . . . . . . . . . 6 2.2 external memory used for audio data storing . . . . . . . . . . . . . . . . . . . . . . 8 3 stm8l/stm8al audio output using on-chip 12-bit dac . . . . . . . . . . . . 9 4 conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 5 references . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 6 revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 AN3225 adpcm codec doc id 17566 rev 3 3/13 1 adpcm codec 1.1 adaptive differential pulse coded modulation (adpcm) adpcm is a fixed length codeword audio codec which reduces redundant information from audio waveforms. the information core is separated from the correlated waveform samples by encoding differences between the current samples and the predicted ones. as the correlation between consecutive audio samples is generally high, this method is reasonably effective and preserves good audio quality. the adpcm codec, which is based on coding waveforms in time domains, is much less complex than codecs based on voice perception (vocoders) that operate in frequency domains. for these reasons, the adpcm codec is the best choice for the audio output on 8-bit microcontrollers. the adpcm algorithm has been developed for speech coding. it is implemented in several steps and is used in teleph one systems like itu-t g.726 (covering ccitt g.721 and g.723 standards). g.726 and similar telecommunication codecs, offer several advanced features which are necessary for successful transmission and streaming. such features include synchronous coding adjustment, tone detection to carry data modem signals, adapted speed control, and recovery from transmission error conditions. these features lead to additional algorithm complexity which either requires the use of a digital signal processor (dsp) or lowering the encoding/decoding sample rate. such features do not bring many additional benefits to the microcontroller for coding/decoding the audio or speech signals. less complex alternatives can be found. 1.2 interactive multimedia association (ima) adpcm the ima (a) adpcm codec is one such alternative solution. this codec is widely used across different computers platforms, for example in microsoft? sound recorder or apple? quicktime? player. it was originally offered by intel/dvi? as an open standard for use by the ima. the reference algorithms and recommended formats were initially developed by the digital audio technical working group (datwg) and refined by the digital audio focus group (dafg) of the ima. these groups are no longer active. the ima datwg reference algorithm is less complex than the g.726 algorithm. the number of encoding/decoding cpu cycles needed is reduced by using fixed prediction and by replacing complex floating point mathemat ical operations by look-up tables. the implementation of the adpcm codec presented in this application not e is compatible with the ima reference algorithm published by the ima datwg/dafg in the ?recommended practices for enhancing digital audio compatib ility in multimedia systems?, revision 3.00 (see references ). a. ima is a computer/audio/video industry trade associat ion that has worked to promote multimedia application development. adpcm codec AN3225 4/13 doc id 17566 rev 3 1.3 ima adpcm decompression the ima adpcm decompression algorithm comp resses samples which are stored as 4-bit signed two?s complement values. the result is decompressed samples in linear 16-bit two?s complement format. the input sample (s q ) is dequantized using an inverse quantizer with an adapted step (q) to obtain a difference (s d ). the step size is adapted by using a lookup table approximating to an exponential function. the vector pointing to the lookup table is adjusted according to the magnitude of s q . to reduce quantization errors, one half of the step size is always added or subtracted to s d . the resulting decompressed linear sample is the sum of s d and the predicted sample (s p ). as s p is a simple zero order hold function, s d is simply added to the previous output value. the decoder preconditions are that s p is cleared and that the quantization step is set to the smallest one. figure 1. adpcm decoder 3 q 3 d 3 , 3 p q ) n v e r s e q u a n t i z e r 0 r e d i c t o r 3 t e p s i z e a d a p t o r a i AN3225 adpcm codec doc id 17566 rev 3 5/13 1.4 ima adpcm compression the ima adpcm compression algorithm encoder calculates a difference (d) between a 16- bit input linear sample (s in ) and a predicted linear sample (s l ). the difference is quantized to a 4-bit compressed output sample (s q ) by using an adapted quantization step (q). this step is the same one as that used by the decoder. figure 2 provides a flowchart of the adpcm encoder. it also shows the decoder building blocks with the functionality described in section 1.3 (see filled light green components). as the encoder covers identical blocks to the decoder and produces identical results, there is no need to store prediction information. the quantized difference is the only core information about the coded samples which needs to be stored or transmitted. figure 2. adpcm encoder ) n v e r s e q u a n t i z e r 3 t e p s i z e a d a p t o r 0 r e d i c t o r 1 u a n t i z e r q q 3 p 3 i n 3 , d 3 d 3 q a i audio data encoding and storing AN3225 6/13 doc id 17566 rev 3 2 audio data encoding and storing the ima adpcm codec is quite popular on pc platforms and a wide range of audio software with different capabilities to process and encode audio data is available. the problematic part of encoded file storage is the file format of the adpcm bitstream as it is not standardized (see ? ima digital audio focus and technical working group? in section 5: references ). on 8-bit microcontrollers, raw data without any formatting is preferred to obtain the easiest manipulation with the coded audio file. on the microsoft windows? platform, a waveform (wav) audio data container is often used to store linear pulse coded modulation (pcm) data. the wav can also be used to store ima adpcm audio data. to use the wav for storing audio bitstream, a wav parser has to be implemented in the microcontroller decoding firmware. this is to unpack raw data so that they can be decompressed. the wav parser increases the complexity and size of the application and does not bring many additional benefits to an 8-bit system. fortunately, there is software available to st ore coded audio bitstream directly as raw data. an example includes the sound exchange (sox) command-line application for audio manipulation distributed under a gnu general purpose license ( http://sox.sourceforge.net ). sox is able to: resample input audio data to any target frequency encode such data in ima adpcm format save output bitstream as unformatted raw data the example command below shows the input parameters used to resample data to: target frequency 15625 hz reduce volume by -12 db compress data by using the ima adpcm codec sox inputfilename -r 15625 outputfilename.ima gain -12 various input file formats can be used, including pcm wav, mp3, mp4, ogg, flac, and many others. see the sox documentation for further details. 2.1 internal flash memory us ed for audio data storing the internal memory of stm8 can be used to store short audio waveforms. one 16-bit pcm sample takes half a byte when compre ssed by ima adpcm codec. the memory requirement can be evaluated using equation 1 . equation 1 for example, a 5-second adpcm bitstream with an 8-khz sample rate uses 20 kbytes of memory. the audio data can be saved to the internal memory by using ?in-application programming? (iap) or the bootloader. see the an2659 (stm8 in-application programming (iap) using a customized user-bootloader) and um0560 (stm8 bootloader) for more details. memory samplerate length 2 ? = AN3225 audio data encoding and storing doc id 17566 rev 3 7/13 another way to store audio data to the internal memory is to link audio data arrays with the compiled code. to include raw data in a project, a binary representation of the data have to be converted to ascii format. any advanced hex editor is able to export data in this format which is then readable by ansi c co mpilers. the txd hexa editor (see references ) is an easy-to-use software, with a free license, no restrictions for commercial use, and which can be freely distributed. the processing of audio data, which have to be included in a project, consists of loading binary data into the editor and exporting them as a c source code. figure 3. hexa editor export to c source code the output of this operation is a c source file with one array: unsigned char rawdata[10340] = { ... this declaration has to be modified, using compiler directives, into an array of constants in the program memory. this is done for the cosmic compiler as follows: const @far unsigned char rawdata[10340] = { ... for the raisonance compiler, the following directive has to be used: fcode unsigned char rawdata[10340] = { ... for more details, see the firmware project associated with this application note on www.st.com/mcu. audio data encoding and storing AN3225 8/13 doc id 17566 rev 3 2.2 external memory used for audio data storing the micro sd card is used for demonstration purposes in this application note for storing audio bitstreams. any other external memory with an interface that is supported by stm8l/stm8al can be used instead. the sd card can be accessed immediately by the operating system if it is formatted by a supported file system i.e. file allocation tabl e (fat) filing system. if the sd card is fat formatted, the microcontroller firmware must also support the fat file system. unfortunately, fat implementation on an 8-bit microcontroller uses too many resources and the footprint is too large for a low-cost oriented microcontro ller application. moreover, it is usually not needed as the audio content of the card is often changed only once. a more convenient way of putting audio data on the sd card is to use specific software which has an embedded driver that bypasses the operation and file system drivers, thereby allowing direct sd card access. a hex editor with such direct access features can be used for modification of the unformatted sd card content. one of the available and tested hex editors can be found in section 5: references (see txd ). when the compressed adpcm data is written to the sd card the mcu firmware can directly read the compressed data samples from the sd card sector by sector. the standard sd card has a fixed sector size of 512 bytes. AN3225 stm8l/stm8al audio output using on-chip 12-bit dac doc id 17566 rev 3 9/13 3 stm8l/stm8al audio output using on-chip 12-bit dac the hardware solution benefits from the stm8l/stm8al 12-bit dac with buffered output. only a few external components are needed to build an application with audio output. a reconstruction low pass filter is needed to reduce the high frequency content in the dac output (see figure 4 ). an audio amplifier is also necessary when boosted power output is required. figure 4. dac output 1. lp = lowpass filter the reconstruction filter should be set up as a lowpass filter with a cutoff frequency lower than half that of the sampling frequency (f sampling ). the purpose of the reconstruction filter is to sufficiently attenuate the aliased and higher harmonic components present in the output frequency spectrum of the dac. the presence of higher harmonic components are caused by a staircase shaped signal of the on-chip dac. it has a standard topology output circuit which implements the zero-order hold function. the higher the band stop attenuation, the lower the output noise. the order of the lowpass (lp) filter determines the steepness (roll- off) of the frequency characteristic transition from passband to stopband. the order of the lp filter is determined by the attenuation at the nyquist frequency (half of the sampling frequency) and the ratio between the nyquist frequency and the cutoff frequency. a common requirement is to keep the noise amplitude over the nyquist frequency (attenuated by the filter) well below quantization noise. figure 5. dac output filtered by rc filter ms30977v1 dac lowpass filter amplifier $ ! # ! - 0 , ) & |